package com.quark.common.security.handler;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.quark.result.RequestResult;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class SsoLogoutSuccessHandler implements LogoutSuccessHandler {

    private static final String REDIRECT_URL = "redirect_url";

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("application/json");
//        response.getWriter().print(JSONUtil.parse(RequestResult.success("退出成功")));
//        response.getWriter().flush();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("message", "退出成功");
        map.put("data", authentication);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(JSONUtil.parse(RequestResult.success(map)).toStringPretty());
        out.flush();
        out.close();
//
//        if (StrUtil.isNotBlank(redirectUrl)) {
//            response.sendRedirect(redirectUrl);
//        } else if (StrUtil.isNotBlank(referer)) {
//            // 默认跳转referer 地址
//            response.sendRedirect(referer);
//        }
//        response.setStatus(HttpStatus.UNAUTHORIZED.value());
//        response.setContentType("application/json;charset=utf-8");
//        response.getWriter().write("退出成功，请重新登录");
    }
}
